package com.jamal2362.apktool.preference;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.widget.NumberPicker;
import androidx.appcompat.app.AlertDialog;
import com.jamal2362.apktool.R;

public class NumberPickerPreference extends CustomPreference
{
	private final int MIN = 8;
	private final int MAX = 40;
	private int dialogTitle;
	private int defaultValue;

	public NumberPickerPreference(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	private boolean initDialog()
	{
		final String key = getKey();
		if (key.equals("napp_fontsz"))
		{
			dialogTitle = R.string.app_fontsz;
			defaultValue = 16;
			return true;
		}
		else if (key.equals("nfont_size"))
		{
			dialogTitle = R.string.font_size;
			defaultValue = 14;
			return true;
		}
		return false;
	}

	@Override
	protected void onClick()
	{
		if (!initDialog())
		{
			return;
		}
		final Context context = getContext();
		final String mKey = getKey();
		final NumberPicker mPicker = new NumberPicker(context);
		mPicker.setMinValue(MIN);
		mPicker.setMaxValue(MAX);
		mPicker.setValue(getSharedPreferences().getInt(mKey, defaultValue));
		final AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
		mBuilder.setView(mPicker);
		mBuilder.setTitle(dialogTitle);
		mBuilder.setNegativeButton(R.string.cancel, null);
		mBuilder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					SharedPreferences.Editor editor = getSharedPreferences().edit();
					editor.putInt(mKey, mPicker.getValue());
					editor.commit();
				}
			});
		mBuilder.setNeutralButton(R.string.search_reset, new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					SharedPreferences.Editor editor = getSharedPreferences().edit();
					editor.putInt(mKey, defaultValue);
					editor.commit();
				}
			});
		mBuilder.create().show();
	}
}
